package com.guigu.tankai.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.liuyunan.pojo.Setmeal;
import com.guigu.tankai.pojo.*;
import com.guigu.tankai.service.*;
import com.guigu.wenzhiyi.service.InterposefollowupvisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
public class Memcontroller {

    @Autowired
    MemService mem;
    @Autowired
    MemberService mm;
    @Autowired
    MemberinfoService m;
    @Autowired
    MemberdynamicinfoService e;
    @Autowired
    MembermedicalhistoryService c;
    @Autowired
    InterposefollowupvisitService r;

    @RequestMapping("tkpageEmp")
    public PageInfo<Member> pageEmp(Integer page, Integer num, String name) {
        PageInfo<Member> leimhcha1 = mem.findByMembertion(page, num, name);
        return leimhcha1;
    }
    @RequestMapping("tkpagememb")
    public PageVo<Member> tkpagememb(Integer page, Integer num, String tankai) {
        Member tMember1 = new Member();
        tMember1.setFilenumber(tankai);
        tMember1.setName(tankai);
        tMember1.setPhonenumber(tankai);
        PageVo<Member> leimhcha1 = mem.findByMembenation(page, num, tMember1);
        return leimhcha1;
    }
    @RequestMapping("tkdelme")
    public int deleteByPrimaryKey(Integer id) {
        return mem.deleteByPrimaryKey(id);
    }
    @RequestMapping("tkAddme")
    public int insertSelective(@RequestBody Addlist addlist) {
        Integer i=0;
        int id = mem.insertSelective(addlist.getMember());

        Memberinfo memberInfo = addlist.getMemberInfo();
        memberInfo.setMemberid(id);
//        memberInfo.setHealthmgrid(hid);
        i+=m.insertSelective(memberInfo);

        Memberdynamicinfo memberDynamicinfo = addlist.getMemberDynamicinfo();
        memberDynamicinfo.setMemberid(id);
        i+=e.insertSelective(memberDynamicinfo);

        Membermedicalhistory memberMedicalhistory = addlist.getMemberMedicalhistory();
        memberMedicalhistory.setMemberid(id);
        i+=c.insertSelective(memberMedicalhistory);
        return i;
    }
    @RequestMapping("tkselid")
    public Member selectByPrimaryKey(Integer id) {
        return mem.selectByPrimaryKey(id);
    }
    @RequestMapping("tkupdme")
    public int updateByPrimaryKey(@RequestBody Member member) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = simpleDateFormat.parse(member.getCreatetime());
        Calendar calendar = Calendar.getInstance();
        long d =  date1.getTime()+1*24*3600*1000;
        calendar.setTimeInMillis(d);
        member.setCreatetime(simpleDateFormat.format(calendar.getTime()));
        return mem.updateByPrimaryKey(member);
    }
    @RequestMapping("tkupdme2")
    public int updateByPrimaryKeySelective(Member record) {
        return mem.updateByPrimaryKeySelective(record);
    }
    @GetMapping("selectByP")
    public List<Setmeal> selectByP(Integer id) {
        return mem.selectByP(id);
    }


    @PostMapping("tkmember")
    public int tkmember() {
        return mm.tkmember().size();
    }
    @PostMapping("tklistme")
    public int tklistme() {
        return mm.tklistme().size();
    }

    @PostMapping("tklist")
    public int  tklist() {
        return mem.tklist().size();
    }
    @PostMapping("tklistee")
    public int  puisit() {
        return r.puisit().size();
    }
    @RequestMapping("tanhytjcha")
    public List<Member> leihytjcha(@RequestParam(defaultValue = "2000-01") String startYear2,
                                   @RequestParam(defaultValue = "2020-01") String endYear2) {
        return mm.tanhytjcha(startYear2, endYear2);
    }
}
